package cn.zhangjie.pet.service.impl;

import cn.zhangjie.basic.service.imp.BasicServiceImpl;
import cn.zhangjie.org.domain.Department;
import cn.zhangjie.pet.domain.Pet;
import cn.zhangjie.pet.domain.PetType;
import cn.zhangjie.pet.mapper.PetTypeMapper;
import cn.zhangjie.pet.service.IPetService;
import cn.zhangjie.pet.service.IPetTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class PetTypeServiceImpl extends BasicServiceImpl<PetType> implements IPetTypeService {
    @Autowired
    private PetTypeMapper petTypeMapper;

    @Override
    public List<PetType> typeTree() {
        //查询所有
        List<PetType> all = petTypeMapper.loadAll();
        //准备一个map
        HashMap<Long,PetType> map = new HashMap<>();
        //把集合放在map中
        for (PetType petType : all) {
            map.put(petType.getId(),petType);
        }
        //准备一个集合保存处理后的数据
        ArrayList<PetType> typeTree = new ArrayList<>();

        for (PetType petType : all) {
            if(petType.getPid()==null){
                typeTree.add(petType);
            }else {
                Long parentpettype = petType.getPid();
                PetType petType1 = map.get(parentpettype);
                petType1.getChildren().add(petType);
            }
        }
        return typeTree;
    }
}
